package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.MessageRedisConstant;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysDialogue;
import com.ruoyi.system.service.ISysDialogueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysDialogueContentMapper;
import com.ruoyi.system.domain.SysDialogueContent;
import com.ruoyi.system.service.ISysDialogueContentService;

/**
 * 对话内容Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-09
 */
@Service
public class SysDialogueContentServiceImpl extends ServiceImpl<SysDialogueContentMapper,SysDialogueContent> implements ISysDialogueContentService
{
    @Autowired
    private SysDialogueContentMapper sysDialogueContentMapper;

    @Autowired
    private ISysDialogueService dialogueService ;

    @Autowired
    private RedisCache redisCache ;

    /**
     * 查询对话内容
     * 
     * @param id 对话内容主键
     * @return 对话内容
     */
    @Override
    public SysDialogueContent selectSysDialogueContentById(Long id)
    {
        return sysDialogueContentMapper.selectSysDialogueContentById(id);
    }

    /**
     * 查询对话内容列表
     * 
     * @param sysDialogueContent 对话内容
     * @return 对话内容
     */
    @Override
    public List<SysDialogueContent> selectSysDialogueContentList(SysDialogueContent sysDialogueContent) {
//        LambdaUpdateWrapper<SysDialogue> wrapper = new LambdaUpdateWrapper<>();
//        wrapper.set(SysDialogue::getUpdateTime ,new Date()) ;
//        wrapper.eq(SysDialogue::getId ,sysDialogueContent.getDialogueId()) ;
//        dialogueService.update(wrapper) ;


        List<SysDialogueContent> list = sysDialogueContentMapper.selectSysDialogueContentList(sysDialogueContent);

        /**清除未读消息数量*/
        Integer allUnReadNumber = redisCache.getCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + SecurityUtils.getUserId());
        if (ObjectUtils.isNotEmpty(allUnReadNumber)) {
            Integer currentDialogueUnReadNumber = redisCache.getCacheObject(MessageRedisConstant.CURRENT_USER_CONTENT_UN_READ_MESSAGE_NUMBER + SecurityUtils.getUserId() + MessageRedisConstant.UNDERLINE + sysDialogueContent.getDialogueId());
            if (ObjectUtils.isNotEmpty(currentDialogueUnReadNumber)) {
                redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + SecurityUtils.getUserId() ,allUnReadNumber - currentDialogueUnReadNumber);
                redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_CONTENT_UN_READ_MESSAGE_NUMBER + SecurityUtils.getUserId() + MessageRedisConstant.UNDERLINE + sysDialogueContent.getDialogueId() ,0) ;
            }
        }

        return list;
    }

    /**
     * 新增对话内容
     * 
     * @param sysDialogueContent 对话内容
     * @return 结果
     */
    @Override
    public int insertSysDialogueContent(SysDialogueContent sysDialogueContent) {
        LambdaQueryWrapper<SysDialogueContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysDialogueContent::getDialogueId ,sysDialogueContent.getDialogueId()) ;
        int count = this.count(wrapper);
        if (StringUtils.isBlank(sysDialogueContent.getContent())) {
            throw new ServiceException("发送内容不能为空!") ;
        }
        sysDialogueContent.setContentSort(count + 1L);
        sysDialogueContent.setCreateTime(DateUtils.getNowDate());
        int i = sysDialogueContentMapper.insert(sysDialogueContent);
        /**修改对话中间表信息*/
//        Long dialogueId = sysDialogueContent.getDialogueId();
//        SysDialogue sysDialogue = new SysDialogue();
//        sysDialogue.setId(dialogueId);
//        sysDialogue.setUpdateTime(new Date());
//        dialogueService.updateById(sysDialogue) ;

        SysDialogue dialogue = dialogueService.getById(sysDialogueContent.getDialogueId());
        Long userId = null ;
        if (SecurityUtils.getUserId().equals(dialogue.getUserId())) {
            userId = dialogue.getOwnerId() ;
        }else {
            userId = dialogue.getUserId() ;
        }
        /**添加未读信息*/
        Integer allUnReadNumber = redisCache.getCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + userId);
        Integer currentDialogueUnReadNumber = redisCache.getCacheObject(MessageRedisConstant.CURRENT_USER_CONTENT_UN_READ_MESSAGE_NUMBER + userId + MessageRedisConstant.UNDERLINE + sysDialogueContent.getDialogueId());

        if (ObjectUtils.isNotEmpty(allUnReadNumber)) {
            redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + userId ,allUnReadNumber + 1);
        }else {
            redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_UN_READ_MESSAGE_NUMBER + userId , 1);
        }
        if (ObjectUtils.isNotEmpty(currentDialogueUnReadNumber)) {
            redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_CONTENT_UN_READ_MESSAGE_NUMBER + userId + MessageRedisConstant.UNDERLINE + sysDialogueContent.getDialogueId() ,currentDialogueUnReadNumber + 1) ;
        }else {
            redisCache.setCacheObject(MessageRedisConstant.CURRENT_USER_CONTENT_UN_READ_MESSAGE_NUMBER + userId + MessageRedisConstant.UNDERLINE + sysDialogueContent.getDialogueId() , 1) ;
        }
        return i;
    }

    /**
     * 修改对话内容
     * 
     * @param sysDialogueContent 对话内容
     * @return 结果
     */
    @Override
    public int updateSysDialogueContent(SysDialogueContent sysDialogueContent)
    {
        sysDialogueContent.setUpdateTime(DateUtils.getNowDate());
        return sysDialogueContentMapper.updateSysDialogueContent(sysDialogueContent);
    }

    /**
     * 批量删除对话内容
     * 
     * @param ids 需要删除的对话内容主键
     * @return 结果
     */
    @Override
    public int deleteSysDialogueContentByIds(Long[] ids)
    {
        return sysDialogueContentMapper.deleteSysDialogueContentByIds(ids);
    }

    /**
     * 删除对话内容信息
     * 
     * @param id 对话内容主键
     * @return 结果
     */
    @Override
    public int deleteSysDialogueContentById(Long id)
    {
        return sysDialogueContentMapper.deleteSysDialogueContentById(id);
    }
}
